package com.cpi.cornpp.sapfiles.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.sapfiles.converter.SAPFileConverter;
import com.cpi.cornpp.common.files.exception.SAPComponentFileException;
import com.cpi.cornpp.sapfiles.view.SAPFileView;
import com.cs.common.init.SAPFileBeanCollection;
import com.cs.common.utils.NumberUtil;
import com.cs.common.utils.ReflectionUtil;

public class SAPFileService {

	private static final String EXISTS = "exists";
	private static final String GET_CONTENT = "getContent";
	private static final String PARSE = "parse";
	private static final String INSERT_ALL = "insertAll";

	/**
	 * Obtiene la lista de archivos disponibles a importar
	 * @return
	 */
	public List list() throws SAPComponentFileException{
		SAPFileConverter converter = new SAPFileConverter();
		ArrayList files = new ArrayList(0);
		Collection fileBeans = SAPFileBeanCollection.getInstance().values();		
		files = converter.convertBeansToViews(fileBeans);
		invokeBaseMethods(files);		
		return files;
	}
	
	/**
	 * Invocar los metodos base de la clase para cada archivo de sap, estos son:
	 * exists y getcontent.
	 * @param files
	 */
	public void invokeBaseMethods(ArrayList files)throws SAPComponentFileException{
		Boolean exist = null;
		SAPFileView fileView = null;
		List list = null;		
		for (Iterator iterator = files.iterator(); iterator.hasNext();) {
			fileView = (SAPFileView) iterator.next();			
			exist = invokeExistMethod(fileView);			
			if(exist!=null && exist.booleanValue()==true){
				fileView.setExist(true);
				list = invokeGetContentMethod(fileView);
				fileView.setContent(list);				
			}
		}
	}
	
	/**
	 * Invocar el metodo existe de la clase del archivo sap en cuestion
	 * @param fileView
	 * @return
	 */
	private Boolean invokeExistMethod(SAPFileView fileView) throws SAPComponentFileException{
		return (Boolean)ReflectionUtil.invokeMethodOnClass(fileView.getClassName(), EXISTS, null, null, fileView.getPhysicalFileName() );
	}
	
	/**
	 * Invocar el metodo getcontent de la clase del archivo sap en cuestion
	 * @param fileView
	 * @return
	 */
	private List invokeGetContentMethod(SAPFileView fileView) throws SAPComponentFileException{
		//Class[] listParamTypes = {List.class};
		//Object[] listParamValues= {fileView.getContent()};
		return (List)ReflectionUtil.invokeMethodOnClass(fileView.getClassName(), GET_CONTENT, null, null,  fileView.getPhysicalFileName()) ;
	}
	
	public void importFiles(List fileViews, String[] ids) throws SAPComponentFileException{
		Integer totalInserted = null;
		final SAPFileView searchView = new SAPFileView();
		SAPFileView auxView = new SAPFileView();
		if(ids!=null){
		for (int i = 0; i < ids.length; i++) {
			searchView.setClassName(ids[i]);
			if(fileViews.contains(searchView)){
				auxView = (SAPFileView)fileViews.get(fileViews.indexOf(searchView));
				List linesPerFile = invokeParseMethod(auxView);
				totalInserted = (Integer)invokeInsertAllMethod(auxView, linesPerFile);
				auxView.setImportedRecords(NumberUtil.convertInteger(totalInserted.intValue()));
			}
		}
		}
		
	}
	
	/**
	 * Invocar el metodo parse de la clase del archivo sap en cuestion
	 * @param fileView
	 * @return
	 */
	private List invokeParseMethod(SAPFileView fileView) throws SAPComponentFileException{
		Class[] listParamTypes = {List.class};
		Object[] listParamValues= {fileView.getContent()};
		return (List)ReflectionUtil.invokeMethodOnClass(fileView.getClassName(), PARSE, listParamTypes, listParamValues,  fileView.getPhysicalFileName());
	}

	
	/**
	 * Invocar el metodo parse de la clase del archivo sap en cuestion
	 * @param fileView
	 * @return
	 */
	private Integer invokeInsertAllMethod(SAPFileView fileView, List dtos) throws SAPComponentFileException{
		Class[] listParamTypes = {List.class};
		Object[] listParamValues= {dtos};
		return (Integer)ReflectionUtil.invokeMethodOnClass(fileView.getClassName(), INSERT_ALL, listParamTypes, listParamValues,  fileView.getPhysicalFileName());
	}
	
	

	
}
